@extends('layouts.header')
@section('title','添加工器具通知规则')
@section('content')
    <article class="page-container">
        <form class="form form-horizontal" id="myform" autocomplete="off">
            <div class="row cl">
                <label class="form-label col-xs-3">工器具：</label>
                <div class="formControls col-xs-4">
                    <select id="tool_id" class="select2 input-text">
                        <option value="">--选择--</option>
                        @foreach($data as $k=>$val)
                            <option value="{{array_first($val)->tool_id}}">{{$k}}</option>
                        @endforeach
                    </select>
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-3">检测标准：</label>
                <div class="formControls col-xs-4">
                    <select class="input-text" id="std_id">
                        <option value="">--先选工器具--</option>
                    </select>
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-3">检测周期：</label>
                <div class="formControls col-xs-4">
                    <input type="text" value="" disabled id="cycle">
                </div>
            </div>

            <div class="row cl">
                <label class="form-label col-xs-3">提前天数：</label>
                <div class="formControls col-xs-9">
                    <input class="input-text" type="number" value="30" min="0" name="early_days">  <small><em>到期前n天开始提醒</em></small>
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-3">间隔天数：</label>
                <div class="formControls col-xs-9">
                    <input class="input-text" type="number" value="31" min="0" name="loop_days">  <small><em>每n天通知一次</em></small>
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-3">到期继续通知天数：</label>
                <div class="formControls col-xs-9">
                    <input class="input-text" type="number" value="0" min="0" name="delay_days">  <small><em>到期n天后不再通知</em></small>
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-3"><b class="c-red">注意：</b></label>
                <div class="formControls col-xs-9 c-red">
                    <small><em>
                            <p>1、如果只提醒一次，必须满足：间隔天数 >（提前天数+推迟天数）</p>
                            <p>2、不能设置成通用规则</p>
                            <p>3、不能全设置为0</p>
                        </em></small>
                </div>
            </div>
            <input type="hidden" value="" name="std_tool_rel_id">
            <input type="hidden" value="add" name="method">
            <div class="row cl pt-20">
                <div class="col-xs-9 col-xs-offset-3">
                    <a class="btn btn-primary radius" id="submit"> 提交 </a>
                    <input class="btn btn-danger radius ml-15" type="button" value="取消" onclick="layer_close();">
                </div>
            </div>
        </form>
    </article>

    <script type="text/javascript">
        var data = @json($data);
        // console.log(data)
        $(function () {
            $('.select2').select2();
            $('#tool_id').on('change', function () {
                let tool_name = $('#tool_id option:checked').text(),stdStr='',options=data[tool_name];
                stdStr += '<option value="">--请选择--</option>';
                for (std_num in options){
                    stdStr += '<option value="'+std_num+'">'+std_num+' </option>'
                }
                $('#std_id').html(stdStr);
            })
            $('#std_id').on('change', function () {
                let std_num = $(this).val(),tool_name = $('#tool_id option:checked').text();
                $('#cycle').val(data[tool_name][std_num].cycle);
                $('input[name=std_tool_rel_id]').val(data[tool_name][std_num].id);
            })
            $('#submit').on('click',function () {
                if($('#myform').valid()){
                    ajax('post','',$('#myform').serialize(),submitBak);
                    function submitBak(res) {
                        if(res == 1){
                            parent.location.reload();
                        }
                    }
                }
            })
        });
    </script>
@endsection
